package com.oig.sys.msg.auth.impl;

import com.oig.auth.api.UserService;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.vo.UserVo;
import com.oig.sys.msg.auth.IDepUserService;
import com.oig.sys.msg.constants.MsgEnums;
import com.oig.sys.msg.dto.NoticeMsgDto;
import com.oig.sys.msg.dto.NoticeMsgReceiverDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
@RequiredArgsConstructor
public class DepUserServiceImpl implements IDepUserService {

    private final UserService userService;


    @Override
    public List<NoticeMsgReceiverDto> changeOrBuildReceiver(NoticeMsgDto noticeMsgDto) {
        if (MsgEnums.ReceiverType.USER.getCode().equals(noticeMsgDto.getReceiverType())){
            if (noticeMsgDto.getNoticeMsgReceiverList()==null){
                List<NoticeMsgReceiverDto> noticeMsgReceiverUserList = new ArrayList<>();
                NoticeMsgReceiverDto receiverDto = new NoticeMsgReceiverDto();
                receiverDto.setReceiverId(noticeMsgDto.getReceiverId());
                receiverDto.setReceiverName(noticeMsgDto.getReceiverName());
                noticeMsgReceiverUserList.add(receiverDto);
                return noticeMsgReceiverUserList;
            } else {
                log.info("noticeMsgReceiverList={}", noticeMsgDto.getNoticeMsgReceiverList());
                return noticeMsgDto.getNoticeMsgReceiverList();
            }
        } else if (MsgEnums.ReceiverType.DEPT.getCode().equals(noticeMsgDto.getReceiverType())){
            log.info("dept receiver:{}",noticeMsgDto.getReceiverName());
            if (noticeMsgDto.getReceiverId()==null){
                throw new BadRequestException("没有接收方");
            }
            List<UserVo> userVoList = this.userService.listAllDeptUser(noticeMsgDto.getReceiverId());
            return this.changeReceiverUser(userVoList);
        } else if (MsgEnums.ReceiverType.TENANT.getCode().equals(noticeMsgDto.getReceiverType())){
            log.info("tenant receiver:{}",noticeMsgDto.getReceiverName());
            if (noticeMsgDto.getReceiverId()==null){
                throw new BadRequestException("没有接收方");
            }
            List<UserVo> userVoList = this.userService.listAllTenUser(noticeMsgDto.getReceiverId());
            return this.changeReceiverUser(userVoList);
        } else if (MsgEnums.ReceiverType.ALL.getCode().equals(noticeMsgDto.getReceiverType())){
            throw new BadRequestException("预留功能，暂时不支持给所有用户发消息");
        }
        return null;
    }

    private List<NoticeMsgReceiverDto> changeReceiverUser(List<UserVo> userVoList){
        if (userVoList==null||userVoList.isEmpty()){
            throw new BadRequestException("没有人员") ;
        }
        List<NoticeMsgReceiverDto> noticeMsgReceiverUserList = new ArrayList<>();
        userVoList.forEach(userVo -> {
            NoticeMsgReceiverDto receiverDto = new NoticeMsgReceiverDto();
            receiverDto.setReceiverId(userVo.getId());
            receiverDto.setReceiverName(userVo.getRealName());
            noticeMsgReceiverUserList.add(receiverDto);
        });
        return noticeMsgReceiverUserList;
    }


    @Override
    public List<String> findUserMobileByUserIds(List<Integer> userIdList) {
        log.debug("findUserMobileByUserIds：{}",userIdList);
        List<UserVo> userVos = userService.loadUsersByUserIds(userIdList);
        if (userVos==null) return null;
        log.debug("findUserMobileByUserIds： return users:{}",userVos);
        return userVos.stream().map(UserVo::getPhone).filter(StringUtils::isNotBlank).collect(Collectors.toList());
    }



}
